package com.lintf.bot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lintf.bot.domain.BotMoney;
import com.lintf.bot.domain.BotMoneyUser;
import com.lintf.bot.domain.constant.Constant;
import com.lintf.bot.domain.dto.BotMoneyAddDTO;
import com.lintf.bot.domain.dto.BotMoneyListDTO;
import com.lintf.bot.domain.dto.BotMoneyUpdateDTO;
import com.lintf.bot.domain.enums.NoticeTypeEnums;
import com.lintf.bot.domain.vo.BotMoneyListVo;
import com.lintf.bot.mapper.BotMoneyMapper;
import com.lintf.bot.notice.NoticeUtils;
import com.lintf.bot.service.IBotMoneyService;
import com.lintf.bot.service.IBotMoneyUserService;
import com.lintf.common.core.domain.entity.SysUser;
import com.lintf.common.utils.SecurityUtils;
import com.lintf.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lintf
 * @description 针对表【bot_money】的数据库操作Service实现
 * @createDate 2024-01-08 21:09:12
 */
@Service
public class BotMoneyServiceImpl extends ServiceImpl<BotMoneyMapper, BotMoney>
        implements IBotMoneyService {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IBotMoneyUserService botMoneyUserService;
    @Autowired
    private NoticeUtils noticeUtils;

    @Override
    public List<BotMoneyListVo> list(BotMoneyListDTO dto) {
        List<BotMoneyListVo> result = new ArrayList<>();
        LambdaQueryWrapper<BotMoney> wrapper = new LambdaQueryWrapper<>();
        if (dto.isOwnData()) {
            wrapper.eq(BotMoney::getUserId, SecurityUtils.getUserId());
        }
        if (dto.getId() != null){
            wrapper.eq(BotMoney::getId,dto.getId());
        }
        if (dto.getGroupId() != null) {
            wrapper.eq(BotMoney::getGroupId, dto.getGroupId());
        } else if (dto.getId() == null){
            wrapper.eq(BotMoney::getGroupId, 0);
        }
        if (StringUtils.isNotEmpty(dto.getSearchValue())) {
            wrapper.like(BotMoney::getMoneyTitle, dto.getSearchValue());
        }
        if (StringUtils.isNotEmpty(dto.getDateSearchValue())) {
            LocalDateTime startTime = LocalDateTime.parse(dto.getDateSearchValue() + "T00:00:00");
            LocalDateTime endTime = LocalDateTime.parse(dto.getDateSearchValue() + "T23:59:59");
            wrapper.ge(BotMoney::getCreateTime, startTime)
                    .le(BotMoney::getCreateTime, endTime);
        }
        wrapper.orderByDesc(BotMoney::getCreateTime);
        Page<BotMoney> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        List<BotMoney> list = baseMapper.selectPage(page, wrapper).getRecords();
        LambdaQueryWrapper<BotMoneyUser> innerUserWrapper;
        BotMoneyListVo vo;
        SysUser sysUser;
        SysUser user = new SysUser();
        List<Long> userIds;
        for (BotMoney botMoney : list) {
            innerUserWrapper = new LambdaQueryWrapper<>();
            innerUserWrapper.eq(BotMoneyUser::getBotMoneyId, botMoney.getId())
                    .ne(BotMoneyUser::getUserId, botMoney.getUserId());
            userIds = botMoneyUserService.list(innerUserWrapper).stream().map(BotMoneyUser::getUserId).collect(Collectors.toList());
            user.setUserIds(userIds);
            vo = new BotMoneyListVo();
            BeanUtils.copyProperties(botMoney, vo);
            sysUser = sysUserService.selectUserById(botMoney.getUserId());
            vo.setAvatar(sysUser.getAvatar())
                    .setNickName(sysUser.getNickName());
            if (!user.getUserIds().isEmpty()) {
                vo.setSysUserList(sysUserService.selectUserListOfNoPage(user));
            }
            result.add(vo);
        }
        return result;
    }

    @Override
    @Transactional
    public void add(BotMoneyAddDTO dto) {
        Long userId = SecurityUtils.getUserId();
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        BotMoney botMoney = new BotMoney();
        BeanUtils.copyProperties(dto, botMoney);
        botMoney.setUserId(userId);
//        botMoney.setCreateParams();
        baseMapper.insert(botMoney);
        String noticeTitle = nickName + "发布了一个" + Constant.MONEY;
        noticeUtils.systemPush(botMoney.getId(), noticeTitle, NoticeTypeEnums.MONEY_PUBLISH, Constant.MONEY, nickName + "的钱袋子", botMoney.getCreateTime(), dto.getGroupId());
    }

    @Override
    public void update(BotMoneyUpdateDTO dto) {
        BotMoney botMoney = new BotMoney();
        BeanUtils.copyProperties(dto, botMoney);
        LambdaQueryWrapper<BotMoneyUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BotMoneyUser::getBotMoneyId, dto.getId());
        botMoneyUserService.remove(wrapper);
        if (dto.getUserIds() != null && !dto.getUserIds().isEmpty()) {
            List<BotMoneyUser> list = new ArrayList<>();
            dto.getUserIds().forEach(item -> {
                BotMoneyUser botMoneyUser = new BotMoneyUser()
                        .setBotMoneyId(dto.getId())
                        .setUserId(item);
//                botMoneyUser.setCreateParams();
                list.add(botMoneyUser);
            });
            botMoneyUserService.saveOrUpdateBatch(list);
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(SecurityUtils.getLoginUser().getUser().getNickName())
                    .append("将")
                    .append(dto.getMoneyTitle())
                    .append("与你一起共享");
            String title = stringBuilder.toString();
            SysUser sysUser = new SysUser();
            sysUser.setUserIds(dto.getUserIds());
            List<SysUser> sysUsers = sysUserService.selectUserList(sysUser);
            noticeUtils.personListPush(dto.getId(), title, NoticeTypeEnums.MONEY_BAG_SHARING, title, new ArrayList<>(sysUsers),null, dto.getGroupId());
        }
//        botMoney.setUpdateParams();
        baseMapper.updateById(botMoney);
    }

    @Override
    public void remove(BotMoneyUpdateDTO dto) {
        BotMoney botMoney = new BotMoney();
        BeanUtils.copyProperties(dto, botMoney);
//        botMoney.setUpdateParams();
        baseMapper.deleteById(botMoney);
    }
}




